package game.util;

import java.nio.FloatBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.util.vector.Matrix3f;
import org.lwjgl.util.vector.Matrix4f;

/**
 * A Simple utility class to deal with matrices
 * 
 * @author Sri Harsha Chilakapati
 * @author Jevgeni Krutov
 */
public class MatrixUtil {

	/**
	 * Converts a Matrix into a FloatBuffer
	 */
	public static FloatBuffer toFloatBuffer(Matrix4f mat) {
		FloatBuffer buffer = BufferUtils.createFloatBuffer(16);
		mat.store(buffer);
		buffer.flip();
		return buffer;
	}

	/**
	 * Generates a Perspective Projection Matrix
	 * 
	 * @param fov
	 *            The vertical field of view
	 * @param aspect
	 *            The aspect ratio
	 * @param zNear
	 *            The near plane
	 * @param zFar
	 *            The far plane
	 * @return
	 */
	public static Matrix4f createPerspectiveProjection(float fov, float aspect, float zNear, float zFar) {
		Matrix4f mat = new Matrix4f();

		float yScale = 1f / (float) Math.tan(Math.toRadians(fov / 2f));
		float xScale = yScale / aspect;
		float frustumLength = zFar - zNear;

		mat.m00 = xScale;
		mat.m11 = yScale;
		mat.m22 = -((zFar + zNear) / frustumLength);
		mat.m23 = -1;
		mat.m32 = -((2 * zFar * zNear) / frustumLength);
		mat.m33 = 0;

		return mat;
	}

	/**
	 * @return A new identity matrix.
	 */
	public static Matrix4f createIdentityMatrix() {
		Matrix4f mat = new Matrix4f();
		mat.setIdentity();
		return mat;
	}
	
	/**
	 * converts Matrix4f to Matrix3f
	 */
	public static Matrix3f getM3f(Matrix4f m4f) {
		Matrix3f m3f = new Matrix3f();
		m3f.m00 = m4f.m00;
		m3f.m01 = m4f.m01;
		m3f.m02 = m4f.m02;
		m3f.m10 = m4f.m10;
		m3f.m11 = m4f.m11;
		m3f.m12 = m4f.m12;
		m3f.m20 = m4f.m20;
		m3f.m21 = m4f.m21;
		m3f.m22 = m4f.m22;
		return m3f;
	}
}
